package com.example.studentmgt.dao;

import com.example.studentmgt.model.Student;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface StudentDao {

    @Select("select * from student")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "age", column = "age"),
            @Result(property = "courseId", column = "courseId")
    })
    List<Student> queryAll();

    //根据id获取student
    @Select("select * from student where id =#{id}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "age", column = "age"),
            @Result(property = "courseId", column = "courseId")
    })
    Student findStudentById(Long id);

    //根据id修改student
    @Update("update student set name=#{name},age=#{age},courseId=#{courseId} WHERE id = #{id} ")
    int updateStudent(Student student);

    //根据id删除用户
    @Delete("delete from student where id = #{id}")
    int deleteStudentById(Long id);

    @Insert("insert into student (name, age) values(#{name},#{age})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void saveStudent(Student student);

    @Select("select * from student order by age limit 10")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "age", column = "age"),
            @Result(property = "courseId", column = "courseId")
    })
    List<Student> top();
}
